package com.ycsj.goldmedalnewconcept.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SPUtils {
	public static final String config = "config";

	// private static final String SP_NAME = "config.xml";

	public static void writeBoolean(Context context, String key, boolean value) {
		getSp(context).edit().putBoolean(key, value).commit();

	}

	public static boolean readBoolean(Context context, String key,
			boolean defValue) {
		return getSp(context).getBoolean(key, defValue);
	}

	private static SharedPreferences getSp(Context context) {
		SharedPreferences sp = context.getSharedPreferences(config,
				Context.MODE_PRIVATE);
		return sp;
	}

	public static SharedPreferences getSharedPreferences(Context ctx) {
		return ctx.getSharedPreferences(config, Activity.MODE_PRIVATE);
	}

	public static String getString(Context ctx, String key) {
		return getSharedPreferences(ctx).getString(key, null);
	}

	public static int getInt(Context ctx, String key) {
		return getSharedPreferences(ctx).getInt(key, 0);
	}

	public static float getFloat(Context ctx, String key) {
		return getSharedPreferences(ctx).getFloat(key, 0);
	}
	public static long getLong(Context ctx, String key) {
		return getSharedPreferences(ctx).getLong(key, 0);
	}
	public static int getInt(Context ctx, String key, int defValue) {
		return getSharedPreferences(ctx).getInt(key, defValue);
	}

	public static boolean getBoolean(Context ctx, String key) {
		return getSharedPreferences(ctx).getBoolean(key, false);
	}

	public static void put(Context ctx, String key, Object object) {
		Editor editor = getSharedPreferences(ctx).edit();
		if (object instanceof Integer) {
			editor.putInt(key, (Integer) object);
		} else if (object instanceof Boolean) {
			editor.putBoolean(key, (Boolean) object);
		} else if (object instanceof String) {
			editor.putString(key, (String) object);
		} else if (object instanceof Float) {
			editor.putFloat(key, (Float) object);
		}else if (object instanceof Long) {
			editor.putLong(key, (Long) object);
		}
		editor.commit();
	}
}
